package com.qumitech.visualnovel.service.music.impl;

import com.alibaba.fastjson.JSONObject;
import com.qumitech.visualnovel.component.exception.BusinessException;
import com.qumitech.visualnovel.component.util.HttpInvokeUtil;
import com.qumitech.visualnovel.component.util.ExcelUtil;
import com.qumitech.visualnovel.model.common.BaseRsp;
import com.qumitech.visualnovel.model.common.ResultEnum;
import com.qumitech.visualnovel.controller.vo.music.MusicSongVO;
import com.qumitech.visualnovel.model.common.UrlConstant;
import com.qumitech.visualnovel.service.music.MusicService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author :  ZhChNing
 * @Descriptiom :
 * @Date Created in 2019/3/13 11:15
 */

@Service
public class MusicServiceImpl implements MusicService {

    private final Logger Log = LoggerFactory.getLogger(this.getClass());
    private final String MUSIC_SONG_ADMIN_URL = "/admin/music/song";


    @Override
    public void addMusicSong(MusicSongVO record) {
        String url = UrlConstant.SERVER_URL + MUSIC_SONG_ADMIN_URL;
        String req = JSONObject.toJSONString(record);
        String body = HttpInvokeUtil.httpPostJson(url, req, UrlConstant.DefaultEncode, null);
        BaseRsp rsp = JSONObject.parseObject(body, BaseRsp.class);
        if (rsp == null || rsp.getStatus() != ResultEnum.SUCCESS.getState()) {
            throw new BusinessException(ResultEnum.FAILURE);
        }
    }

    @Override
    public void updateMusicSong(MusicSongVO record) {
        String url = UrlConstant.SERVER_URL + MUSIC_SONG_ADMIN_URL + "/" + record.getSongId();
        String req = JSONObject.toJSONString(record);
        String body = HttpInvokeUtil.httpPutJson(url, req, UrlConstant.DefaultEncode, null);
        BaseRsp rsp = JSONObject.parseObject(body, BaseRsp.class);
        if (rsp == null || rsp.getStatus() != ResultEnum.SUCCESS.getState()) {
            throw new BusinessException(ResultEnum.FAILURE);
        }


    }

    @Override
    public void removeMusicSong(String songId) {
        String url = UrlConstant.SERVER_URL + MUSIC_SONG_ADMIN_URL + "/" + songId;
        Map<String, String> headMap = new HashMap<>();
        String body = HttpInvokeUtil.httpDelete(url, headMap);
        BaseRsp rsp = JSONObject.parseObject(body, BaseRsp.class);
        if (rsp == null || rsp.getStatus() != ResultEnum.SUCCESS.getState()) {
            throw new BusinessException(ResultEnum.FAILURE);
        }
    }

    @Async
    @Override
    public void importMusicSong(MultipartFile file) {
        Map<Integer, Map<Integer, Object>> map = new HashMap<>();
        try {
            map = ExcelUtil.readExcelContentz(file);
        } catch (Exception e) {
            Log.error("[importMusicSong] import excel fail,{}", e);
        }

        String url = UrlConstant.SERVER_URL + MUSIC_SONG_ADMIN_URL;

        for (Integer key : map.keySet()) {
            Map<Integer, Object> music = map.get(key);
            try {
                String songType = (String) music.get(0);
                String songName = (String) music.get(1);
                String singer = (String) music.get(2);
                String sizes = (String) music.get(3);
                String voiceUrl = (String) music.get(4);

                Double size = Double.valueOf(sizes.replace("M", ""));
                Log.info("[------------]songName:{}----singer:{}----size:{}------voiceUrl:{}-----------songType：{}", songName, singer, size, voiceUrl, songType);

                Map<String, String> requestParams = new HashMap<>();
                requestParams.put("song_name", songName);
                requestParams.put("singer", singer);
                requestParams.put("size", String.valueOf((int) (size * 1024)));
                requestParams.put("voice_url", voiceUrl);
                requestParams.put("song_type", String.valueOf(Double.valueOf(songType).intValue()));
                Log.info("[importMusicSong] request, url:{}, param:{}", url, requestParams);
                String body = HttpInvokeUtil.httpPostJson(url, requestParams, UrlConstant.DefaultEncode);
                Log.info("[importMusicSong] response :{}", body);
                BaseRsp rsp = JSONObject.parseObject(body, BaseRsp.class);
                if (rsp == null || rsp.getStatus() != ResultEnum.SUCCESS.getState()) {
                    throw new BusinessException(ResultEnum.FAILURE);
                }
            } catch (Exception e) {
                Log.error("[importMusicSong] add music fail. name:{}, error:{}", music, e);
            }
        }
    }

    @Override
    public Map<String, Object> listMusicSongVO(Integer offset, Integer limit, Byte subject, String songId, String songName, Integer songType, String voiceUrl, String lyricUrl, String author, String singer, Integer size, String tag, Byte inUse) {

        String url = UrlConstant.SERVER_URL + MUSIC_SONG_ADMIN_URL+"/list";
        Map<String, String> paramMap = new HashMap<>();
        if (offset != null)
            paramMap.put("offset", offset.toString());
        if (limit != null)
            paramMap.put("limit", limit.toString());
        if (subject != null)
            paramMap.put("subject", subject.toString());
        if (songId != null)
            paramMap.put("song_id", songId);
        if (songName != null)
            paramMap.put("song_name", songName);
        if (songType != null)
            paramMap.put("song_type", songType.toString());
        if (voiceUrl != null)
            paramMap.put("voice_url", voiceUrl);
        if (lyricUrl != null)
            paramMap.put("lyric_url", lyricUrl);
        if (author != null)
            paramMap.put("singer", author);
        if (singer != null)
            paramMap.put("singer", singer);
        if (size != null)
            paramMap.put("size", size.toString());
        if (tag != null)
            paramMap.put("tag", tag);
        if (inUse != null)
            paramMap.put("in_use", inUse.toString());


        String body = HttpInvokeUtil.httpGet(url, paramMap, null);
        BaseRsp rsp = JSONObject.parseObject(body, BaseRsp.class);
        if (rsp == null || rsp.getStatus() != ResultEnum.SUCCESS.getState()
                || rsp.getData() == null) {
            throw new BusinessException(ResultEnum.FAILURE);
        }
        Map<String, Object> rspMap = (Map<String, Object>) rsp.getData();
        return rspMap;

    }

}
